package BuildPage::App;

use strict;
use base qw( MT::App::CMS );
use Data::Dumper;

sub doLog {
    my ($msg) = @_; 
    return unless defined($msg);

    use MT::Log;
    my $log = MT::Log->new;
    $log->message($msg) ;
    $log->save or die $log->errstr;
}

sub init {
    my $app = shift;
    my %param = @_;
    $app->SUPER::init(%param) or return;
    $app->add_methods(
        'view' => \&hdlr_view,
    );
    $app->{default_mode} = 'view';
    $app->{plugin_template_path} = 'tmpl';
    $app->{requires_login} = 0;
    return $app;
}

sub plugin {
    MT::Plugin::BuildPage->instance;
}

sub script_url {
    MT::Plugin::BuildPage->instance->url;    
}
sub translate {
    my $app = shift;
    $app->plugin->translate(@_);
}

sub translate_templatized {
    my $app = shift;
    $app->plugin->translate_templatized(@_);
}

sub build_page {
    my $app = shift;
    my ($page, $param) = @_;
    my $q = $app->param;

    $param->{plugin_name} = $MT::Plugin::BuildPage::PLUGIN_NAME;
    $param->{plugin_version} = $MT::Plugin::BuildPage::VERSION;
    $app->SUPER::build_page($page, $param);
}

sub hdlr_view {
    my $app = shift;
    my %param = $_[0] ? %{ $_[0] } : ();
    my $q = $app->param;

    my $return_url = $app->uri;

    require MT::Template;
    my $tmpl = MT::Template->load ({ blog_id => 0, name => 'TEST' })
        or return;
    $app->build_page($tmpl, { type => $q->{ '_type'} } );
}

1;
